package com.example.sunny.service.dao

import android.util.Log
import com.example.sunny.pojo.weather.DailyResponse
import org.litepal.LitePal
import org.litepal.extension.findAll
import org.litepal.extension.saveAll

object WeatherDao {

    fun saveDaily(daily: DailyResponse.Daily){
        daily.save()
        daily.temperature.saveAll()
        daily.skycon.saveAll()
        daily.lifeIndex.save()
        daily.lifeIndex.coldRisk.saveAll()
        daily.lifeIndex.carWashing.saveAll()
        daily.lifeIndex.ultraviolet.saveAll()
        daily.lifeIndex.dressing.saveAll()
        daily.airQuality.save()
        daily.airQuality.aqi.saveAll()
        for (avgAQI in daily.airQuality.aqi) {
            avgAQI.avg.save()
        }
    }

    fun deleteDaily(daily: DailyResponse.Daily){

        LitePal.apply {

            deleteAll(DailyResponse.Standard::class.java)
            deleteAll(DailyResponse.AvgAQI::class.java)
            deleteAll(DailyResponse.AQI::class.java)
            for (aqi in daily.airQuality.aqi) {
                aqi.avg.clearSavedState()
                aqi.clearSavedState()
            }
            daily.airQuality.clearSavedState()
            markAsDeleted(daily.airQuality.aqi)

            deleteAll(DailyResponse.Dressing::class.java)
            markAsDeleted(daily.lifeIndex.dressing)
            deleteAll(DailyResponse.Ultraviolet::class.java)
            markAsDeleted(daily.lifeIndex.ultraviolet)
            deleteAll(DailyResponse.CarWashing::class.java)
            markAsDeleted(daily.lifeIndex.carWashing)
            deleteAll(DailyResponse.ColdRisk::class.java)
            markAsDeleted(daily.lifeIndex.coldRisk)
            deleteAll(DailyResponse.LifeIndex::class.java)
            daily.lifeIndex.clearSavedState()
            deleteAll(DailyResponse.Skycon::class.java)
            markAsDeleted(daily.skycon)
            deleteAll(DailyResponse.Temperature::class.java)
            markAsDeleted(daily.temperature)
            deleteAll(DailyResponse.Daily::class.java)
            daily.clearSavedState()
        }


    }

    fun getDaily():DailyResponse.Daily{
        val listStandard = LitePal.findAll<DailyResponse.Standard>()
        val listDressing = LitePal.findAll<DailyResponse.Dressing>()
        val listUltraviolet = LitePal.findAll<DailyResponse.Ultraviolet>()
        val listCarWashing = LitePal.findAll<DailyResponse.CarWashing>()
        val listColdRisk = LitePal.findAll<DailyResponse.ColdRisk>()
        val listSkycon = LitePal.findAll<DailyResponse.Skycon>()
        val listTemperature = LitePal.findAll<DailyResponse.Temperature>()

        val list = ArrayList<DailyResponse.AvgAQI>()
        for (standard in listStandard) {
            list.add(DailyResponse.AvgAQI(standard))
        }
        val aqi = DailyResponse.AQI(list)

        val lifeIndex = DailyResponse.LifeIndex(listColdRisk,listCarWashing,listUltraviolet,listDressing)

        val daily = DailyResponse.Daily(listTemperature,listSkycon,lifeIndex,aqi)
        Log.d("select result",daily.toString())
        return daily
    }

}